﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using BrainTechLLC;
using BrainTechLLC.ThreadSafeObjects;
using BrainTechLLC.WPF;
using DragDropLibrary;
using ImageAppDataModels;
using ImageAppViewModels;
using BrainTechLLC.ImageOperatorsNonAForgeDependent;
using BrainTechLLC.ImageOperators;
using BrainTechLLC.FolderExplorer;
using TemporalFilterExtensions;
using System.Drawing;
using System.Windows.Media;
using BrainTechLLC.FilterExtensions;
using System.Windows.Data;
using System.Collections.ObjectModel;
using System.Reflection;
using System.ComponentModel;

namespace WpfImageDuplicates
{
    public partial class ImageCollectionView : UserControl
    {
        public bool _AllWillBeInvisibleFromHereOn;

        // Using a DependencyProperty as the backing store for Source.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SourceProperty =
            DependencyProperty.Register("Source", typeof(CollectionViewSource), typeof(ImageCollectionView), new UIPropertyMetadata(null));

        public static readonly DependencyProperty FileImageCollectionViewModelProperty =
            DependencyProperty.Register("FileImageCollectionViewModel", typeof(FileImageCollectionViewModel), typeof(ImageCollectionView), new PropertyMetadata(null, OnFileImageCollectionViewModelPropertyChanged));

        public CollectionViewSource Source
        {
            get { return (CollectionViewSource)GetValue(SourceProperty); }
            set { SetValue(SourceProperty, value); }
        }

        public FileImageCollectionViewModel FileImageCollectionViewModel
        {
            get { return (FileImageCollectionViewModel)GetValue(FileImageCollectionViewModelProperty); }
            set { _vm = value; SetValue(FileImageCollectionViewModelProperty, value); }
        }

        private static void OnFileImageCollectionViewModelPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ImageCollectionView source = d as ImageCollectionView;
            FileImageCollectionViewModel vNew = e.NewValue as FileImageCollectionViewModel;
            FileImageCollectionViewModel vOld = e.OldValue as FileImageCollectionViewModel;
            source.UpdateFileImageCollectionViewModel(vNew, vOld);
        }

        private void UpdateFileImageCollectionViewModel(FileImageCollectionViewModel viewModel, FileImageCollectionViewModel viewModelOld)
        {
            if (viewModelOld != null)
            {
                viewModelOld.ThumbnailsInvalidated -= FileImageCollectionViewModel_ThumbnailsInvalidated;
                viewModelOld.WantRefreshDirectories -= FileImageCollectionViewModel_WantRefreshDirectories;
                viewModelOld.CollectionChanged -= FileImageCollectionViewModel_CollectionChanged;
                viewModelOld.ClearingAll -= FileImageCollectionViewModel_ClearingAll;
                viewModelOld.CollectionChanged -= viewModel_CollectionChanged;
                Settings.Values.PropertyChanged -= Values_PropertyChanged;
                if (Source != null)
                {
                    Source.Source = null;
                    Source = null;
                }
            }

            _vm = viewModel;

            if (viewModel != null)
            {
                loadingGrid.DataContext = viewModel;

                Source = new CollectionViewSource()
                {
                    Source = Displayed,
                };

                _lastSelected = viewModel.Count > 0 ? viewModel[0] : null;
                viewModel.ClearingAll += new EventHandler(FileImageCollectionViewModel_ClearingAll);
                viewModel.ThumbnailsInvalidated += new EventHandler(FileImageCollectionViewModel_ThumbnailsInvalidated);
                viewModel.WantRefreshDirectories += new EventHandler(FileImageCollectionViewModel_WantRefreshDirectories);
                viewModel.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(FileImageCollectionViewModel_CollectionChanged);
                Settings.Values.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(Values_PropertyChanged);
                viewModel.CollectionChanged += new NotifyCollectionChangedEventHandler(viewModel_CollectionChanged);
                loadingGrid.Visibility = System.Windows.Visibility.Collapsed;
            }

            _AllWillBeInvisibleFromHereOn = false;
            SetSourceToDisplayed();
        }

        public void SetSourceToDisplayed()
        {
            if (Source == null)
            {
                Source = new CollectionViewSource()
                {
                    Source = Displayed,
                };
            }
            _AllWillBeInvisibleFromHereOn = false;
            Binding b = new Binding() { Source = Source, Mode = BindingMode.OneWay };
            lstItems.SetBinding(ListBox.ItemsSourceProperty, b);            
        }
        
        internal void DecoupleEvents()
        {
            Settings.Values.PropertyChanged -= Values_PropertyChanged;
            Source = null;
            Displayed.Clear();
            DisplayedAlready.Clear();
            Rectangles.Clear();
            if (Timer != null)
            {
                Timer.DisposeTimer();
            }
            Timer = null;
            _wantView = null;
            if (_dirtyTimer != null)
            {
                _dirtyTimer.DisposeTimer();
            }
            if (UpdateTimer != null && UpdateTimer._timer != null)
            {
                UpdateTimer._timer.Dispose();
            }
            if (UpdateTimer != null)
            {
                UpdateTimer._collectionView = null;
                UpdateTimer._scrollHandler = null;
            }
            if (ThumbsHandler != null)
            {
                ThumbsHandler._parentView = null;
                if (ThumbsHandler.ThumbPreloader != null)
                {
                    ThumbsHandler.ThumbPreloader.Stop();
                }
            }
            FileImageCollectionViewModel = null;
            UpdateTimer = null;
            ThumbsHandler = null;
            _OrderedFileNames = null;
            _CounterIndexes = null;
            _wrapPanel = null;
            BlurHandler = null;
            ComparisonHandler = null;
            Source = null;
            tracker = null;
        }
    }
}